package generator.website;

import java.util.Iterator;
import org.eclipse.emf.common.util.EList;
import CarProject.*;
import CarProject.impl.*;;

public class DAO
{
  protected static String nl;
  public static synchronized DAO create(String lineSeparator)
  {
    nl = lineSeparator;
    DAO result = new DAO();
    nl = null;
    return result;
  }

  public final String NL = nl == null ? (System.getProperties().getProperty("line.separator")) : nl;
  protected final String TEXT_1 = "\t" + NL + "import javax.naming.Context;" + NL + "import javax.naming.InitialContext;" + NL + "import javax.naming.NamingException;" + NL + "import javax.sql.DataSource;" + NL + "" + NL + "import java.sql.Connection;" + NL + "import java.sql.PreparedStatement;" + NL + "import java.sql.SQLException;" + NL + "" + NL + "" + NL + "public class DAO";
  protected final String TEXT_2 = "{" + NL + "\tprivate final static String RESOURCE_JDBC = \"java:comp/env/jdbc/dsMyDB\";" + NL + "\tpublic DAO";
  protected final String TEXT_3 = "(){}" + NL + "\t";
  protected final String TEXT_4 = NL + "\t\tpublic String add";
  protected final String TEXT_5 = "(";
  protected final String TEXT_6 = " ";
  protected final String TEXT_7 = " ";
  protected final String TEXT_8 = ",";
  protected final String TEXT_9 = NL + "\t";
  protected final String TEXT_10 = " ";
  protected final String TEXT_11 = "){" + NL + "\t\ttry {" + NL + "\t\t\tfinal Context lContext = new InitialContext();" + NL + "\t\t\tfinal DataSource lDataSource = (DataSource) lContext" + NL + "\t\t\t\t\t.lookup(RESOURCE_JDBC);" + NL + "\t\t\tfinal Connection lConnection = lDataSource.getConnection();" + NL + "" + NL + "\t\t\t// adding a new contact" + NL + "\t\t\tfinal PreparedStatement lPreparedStatementCreation =" + NL + "" + NL + "\t\t\tlConnection" + NL + "\t\t\t\t\t.prepareStatement(\"INSERT INTO ";
  protected final String TEXT_12 = "(";
  protected final String TEXT_13 = ",";
  protected final String TEXT_14 = ")VALUES (";
  protected final String TEXT_15 = "?,";
  protected final String TEXT_16 = "?)\");\t\t\t\t\t\t\t\t" + NL + "\t\t\t";
  protected final String TEXT_17 = "\t\t\t\t" + NL + "\t\t\tlPreparedStatementCreation.set";
  protected final String TEXT_18 = "(";
  protected final String TEXT_19 = ", ";
  protected final String TEXT_20 = ");";
  protected final String TEXT_21 = NL + "\t\t\tlPreparedStatementCreation.set";
  protected final String TEXT_22 = "(";
  protected final String TEXT_23 = ", ";
  protected final String TEXT_24 = ");" + NL + "\t\t\tlPreparedStatementCreation.executeUpdate();" + NL + "\t\t\treturn null;" + NL + "\t\t} catch (NamingException e) {" + NL + "" + NL + "\t\t\treturn \"NamingException : \" + e.getMessage();" + NL + "" + NL + "\t\t} catch (SQLException e) {" + NL + "" + NL + "\t\t\treturn \"SQLException : \" + e.getMessage();" + NL + "\t\t}" + NL + "\t}" + NL + "\t" + NL + "\t// Modifying a contact (Primary key of table is first argument)" + NL + "\tpublic String modify";
  protected final String TEXT_25 = "(";
  protected final String TEXT_26 = " ";
  protected final String TEXT_27 = " ";
  protected final String TEXT_28 = ",";
  protected final String TEXT_29 = NL + "\t";
  protected final String TEXT_30 = " ";
  protected final String TEXT_31 = "){" + NL + "\t" + NL + "\ttry {" + NL + "\t\t\tfinal Context lContext = new InitialContext();" + NL + "\t\t\tfinal DataSource lDataSource = (DataSource) lContext" + NL + "\t\t\t\t\t.lookup(RESOURCE_JDBC);" + NL + "\t\t\tfinal Connection lConnection = lDataSource.getConnection();" + NL + "" + NL + "\t\t\t// modify a new contact" + NL + "\t\t\tfinal PreparedStatement lPreparedStatementCreation =" + NL + "\t\t\t";
  protected final String TEXT_32 = NL + "\t\t\tlConnection" + NL + "\t\t\t\t\t.prepareStatement(\"UPDATE ";
  protected final String TEXT_33 = " SET ";
  protected final String TEXT_34 = "=:";
  protected final String TEXT_35 = ",";
  protected final String TEXT_36 = " ";
  protected final String TEXT_37 = "=:";
  protected final String TEXT_38 = " WHERE ";
  protected final String TEXT_39 = " = :0)\");" + NL + "\t\t\t\t\t" + NL + "\t\t\t\t\t" + NL + "\t\t\t\t\t" + NL + "\t\t\t";
  protected final String TEXT_40 = "\t\t\t\t" + NL + "\t\t\tlPreparedStatementCreation.set";
  protected final String TEXT_41 = "(";
  protected final String TEXT_42 = ", ";
  protected final String TEXT_43 = ");";
  protected final String TEXT_44 = NL + "\t\t\tlPreparedStatementCreation.set";
  protected final String TEXT_45 = "(";
  protected final String TEXT_46 = ", ";
  protected final String TEXT_47 = ");" + NL + "\t\t\tlPreparedStatementCreation.executeUpdate();\t\t" + NL + "\t\t\t\treturn null;" + NL + "\t\t} catch (NamingException e) {" + NL + "" + NL + "\t\t\treturn \"NamingException : \" + e.getMessage();" + NL + "\t\t\t" + NL + "" + NL + "\t\t} catch (SQLException e) {" + NL + "" + NL + "\t\t\treturn \"SQLException : \" + e.getMessage();" + NL + "\t\t}" + NL + "\t}" + NL + "\t" + NL + "\t" + NL + "\tpublic String delete";
  protected final String TEXT_48 = "(";
  protected final String TEXT_49 = " ";
  protected final String TEXT_50 = "\t";
  protected final String TEXT_51 = "){" + NL + "\t\ttry {" + NL + "\t\t\tfinal Context lContext = new InitialContext();" + NL + "\t\t\tfinal DataSource lDataSource = (DataSource) lContext" + NL + "\t\t\t\t\t.lookup(RESOURCE_JDBC);" + NL + "\t\t\tfinal Connection lConnection = lDataSource.getConnection();" + NL + "" + NL + "\t\t\t// modify a new contact" + NL + "\t\t\tfinal PreparedStatement lPreparedStatementCreation =" + NL + "\t" + NL + "\t\t\tlConnection" + NL + "\t\t\t\t\t.prepareStatement(\"DELETE FROM ";
  protected final String TEXT_52 = " WHERE ";
  protected final String TEXT_53 = "=:0\");" + NL + "\t\t\t\t\t" + NL + "\t\t\t";
  protected final String TEXT_54 = "\t\t\t\t" + NL + "\t\t\tlPreparedStatementCreation.set";
  protected final String TEXT_55 = "(0, ";
  protected final String TEXT_56 = ");" + NL + "\t\t\tlPreparedStatementCreation.executeUpdate();" + NL + "\t\t\t" + NL + "\t\t\treturn null;" + NL + "\t\t} catch (NamingException e) {" + NL + "" + NL + "\t\t\treturn \"NamingException : \" + e.getMessage();" + NL + "\t\t\t" + NL + "" + NL + "\t\t} catch (SQLException e) {" + NL + "" + NL + "\t\t\treturn \"SQLException : \" + e.getMessage();" + NL + "\t\t}" + NL + "\t" + NL + "\t}" + NL + "\t" + NL + "\tpublic String list";
  protected final String TEXT_57 = "(";
  protected final String TEXT_58 = " ";
  protected final String TEXT_59 = " ";
  protected final String TEXT_60 = ",";
  protected final String TEXT_61 = NL + "\t";
  protected final String TEXT_62 = " ";
  protected final String TEXT_63 = "){" + NL + "\treturn null;" + NL + "\t}" + NL + "\t" + NL + "\t" + NL + "\t" + NL + "} ";

  public String generate(Object argument)
  {
    final StringBuffer stringBuffer = new StringBuffer();
     Table laTable = (Table) argument; 
    EList<Champs> listChamps = laTable.getPossede();
     Champs primaryKey = laTable.getClePrimaire().getChamps();
    String type=null;
    stringBuffer.append(TEXT_1);
    stringBuffer.append(laTable.getNom());
    stringBuffer.append(TEXT_2);
    stringBuffer.append(laTable.getNom());
    stringBuffer.append(TEXT_3);
    int i;
    stringBuffer.append(TEXT_4);
    stringBuffer.append(laTable.getNom());
    stringBuffer.append(TEXT_5);
    for (i = 0;i<listChamps.size()-1;i++){
if(((ChampsImpl)laTable.getPossede().get(i)).getType().getValue()== EnumType.VARCHAR){ type = "String"; } else { type = "long"; }
	
    stringBuffer.append(TEXT_6);
    stringBuffer.append(type);
    stringBuffer.append(TEXT_7);
    stringBuffer.append(((ChampsImpl)laTable.getPossede().get(i)).getNom());
    stringBuffer.append(TEXT_8);
    }
    if(((ChampsImpl)laTable.getPossede().get(i)).getType().getValue()== EnumType.VARCHAR){ type = "String"; } else { type = "long"; }
    stringBuffer.append(TEXT_9);
    stringBuffer.append(type);
    stringBuffer.append(TEXT_10);
    stringBuffer.append(((ChampsImpl)laTable.getPossede().get(i)).getNom());
    stringBuffer.append(TEXT_11);
    stringBuffer.append(laTable.getNom());
    stringBuffer.append(TEXT_12);
    for (i = 0;i<listChamps.size()-1;i++){
    stringBuffer.append((listChamps.get(i)).getNom());
    stringBuffer.append(TEXT_13);
    }
    stringBuffer.append((listChamps.get(i)).getNom());
    stringBuffer.append(TEXT_14);
    for (i = 0;i<listChamps.size()-1;i++){
    stringBuffer.append(TEXT_15);
    }
    stringBuffer.append(TEXT_16);
    for (i = 0;i<listChamps.size()-1;i++){
			if(((ChampsImpl)laTable.getPossede().get(i)).getType().getValue()== EnumType.VARCHAR){ type = "String"; } else { type = "long"; }
    stringBuffer.append(TEXT_17);
    stringBuffer.append(type);
    stringBuffer.append(TEXT_18);
    stringBuffer.append(i);
    stringBuffer.append(TEXT_19);
    stringBuffer.append((listChamps.get(i)).getNom());
    stringBuffer.append(TEXT_20);
    }
    
			if(((ChampsImpl)laTable.getPossede().get(i)).getType().getValue()== EnumType.VARCHAR){ type = "String"; } else { type = "long"; }
    stringBuffer.append(TEXT_21);
    stringBuffer.append(type);
    stringBuffer.append(TEXT_22);
    stringBuffer.append(i);
    stringBuffer.append(TEXT_23);
    stringBuffer.append((listChamps.get(i)).getNom());
    stringBuffer.append(TEXT_24);
    stringBuffer.append(laTable.getNom());
    stringBuffer.append(TEXT_25);
    for (i = 0;i<listChamps.size()-1;i++){
if(((ChampsImpl)laTable.getPossede().get(i)).getType().getValue()== EnumType.VARCHAR){ type = "String"; } else { type = "long"; }
	
    stringBuffer.append(TEXT_26);
    stringBuffer.append(type);
    stringBuffer.append(TEXT_27);
    stringBuffer.append(((ChampsImpl)laTable.getPossede().get(i)).getNom());
    stringBuffer.append(TEXT_28);
    }
    if(((ChampsImpl)laTable.getPossede().get(i)).getType().getValue()== EnumType.VARCHAR){ type = "String"; } else { type = "long"; }
    stringBuffer.append(TEXT_29);
    stringBuffer.append(type);
    stringBuffer.append(TEXT_30);
    stringBuffer.append(((ChampsImpl)laTable.getPossede().get(i)).getNom());
    stringBuffer.append(TEXT_31);
     Champs inter=null;
    stringBuffer.append(TEXT_32);
    stringBuffer.append(laTable.getNom());
    stringBuffer.append(TEXT_33);
    for (i = 0;i<listChamps.size()-1;i++){
					if(listChamps.get(i).getNom() == primaryKey.getNom()){ inter = primaryKey;}
					else{
    stringBuffer.append(listChamps.get(i).getNom());
    stringBuffer.append(TEXT_34);
    stringBuffer.append(i);
    stringBuffer.append(TEXT_35);
    }}
    stringBuffer.append(TEXT_36);
    stringBuffer.append(listChamps.get(i).getNom());
    stringBuffer.append(TEXT_37);
    stringBuffer.append(i);
    stringBuffer.append(TEXT_38);
    stringBuffer.append(inter.getNom());
    stringBuffer.append(TEXT_39);
    for (i = 0;i<listChamps.size()-1;i++){
			if(((ChampsImpl)laTable.getPossede().get(i)).getType().getValue()== EnumType.VARCHAR){ type = "String"; } else { type = "long"; }
    stringBuffer.append(TEXT_40);
    stringBuffer.append(type);
    stringBuffer.append(TEXT_41);
    stringBuffer.append(i);
    stringBuffer.append(TEXT_42);
    stringBuffer.append((listChamps.get(i)).getNom());
    stringBuffer.append(TEXT_43);
    }
    
			if(((ChampsImpl)laTable.getPossede().get(i)).getType().getValue()== EnumType.VARCHAR){ type = "String"; } else { type = "long"; }
    stringBuffer.append(TEXT_44);
    stringBuffer.append(type);
    stringBuffer.append(TEXT_45);
    stringBuffer.append(i);
    stringBuffer.append(TEXT_46);
    stringBuffer.append((listChamps.get(i)).getNom());
    stringBuffer.append(TEXT_47);
    stringBuffer.append(laTable.getNom());
    stringBuffer.append(TEXT_48);
    if(primaryKey.getType().getValue()== EnumType.VARCHAR){ type = "String"; } else { type = "long"; }
    stringBuffer.append(TEXT_49);
    stringBuffer.append(type);
    stringBuffer.append(TEXT_50);
    stringBuffer.append(primaryKey.getNom());
    stringBuffer.append(TEXT_51);
    stringBuffer.append(laTable.getNom());
    stringBuffer.append(TEXT_52);
    stringBuffer.append(listChamps.get(0).getNom());
    stringBuffer.append(TEXT_53);
    if(((ChampsImpl)laTable.getPossede().get(0)).getType().getValue()== EnumType.VARCHAR){ type = "String"; } else { type = "long"; }
    stringBuffer.append(TEXT_54);
    stringBuffer.append(type);
    stringBuffer.append(TEXT_55);
    stringBuffer.append((listChamps.get(0)).getNom());
    stringBuffer.append(TEXT_56);
    stringBuffer.append(laTable.getNom());
    stringBuffer.append(TEXT_57);
    for (i = 0;i<listChamps.size()-1;i++){
if(((ChampsImpl)laTable.getPossede().get(i)).getType().getValue()== EnumType.VARCHAR){ type = "String"; } else { type = "long"; }
	
    stringBuffer.append(TEXT_58);
    stringBuffer.append(type);
    stringBuffer.append(TEXT_59);
    stringBuffer.append(((ChampsImpl)laTable.getPossede().get(i)).getNom());
    stringBuffer.append(TEXT_60);
    }
    if(((ChampsImpl)laTable.getPossede().get(i)).getType().getValue()== EnumType.VARCHAR){ type = "String"; } else { type = "long"; }
    stringBuffer.append(TEXT_61);
    stringBuffer.append(type);
    stringBuffer.append(TEXT_62);
    stringBuffer.append(((ChampsImpl)laTable.getPossede().get(i)).getNom());
    stringBuffer.append(TEXT_63);
    return stringBuffer.toString();
  }
}
